/**************************************************************************************
*  MemoryRegion.c
*
*  This file implements all native methods declared in the MemoryRegion class.
*
* NOTE:
*  Be careful when accessing the native methods arguments. If native method is
*  declared as static then the first argoment starts at index zero. Non-static native
*  methods have arguments starting at index one, while argument at index zero is
*  a reference to the object on which the method is being invoked.
**************************************************************************************
*
* This file is covered by the GNU GPL with the following exception:
*   As a special exception, the copyright holders of this library give you permission
*   to link this library with independent modules to produce an executable, regardless
*   of the license terms of these independent modules, and to copy and distribute the
*   resulting executable under terms of your choice, provided that you also meet, for
*   each linked independent module, the terms and conditions of the license of that
*   module. An independent module is a module which is not derived from or based on
*   this library. If you modify this library, you may extend this exception to your
*   version of the library, but you are not obligated to do so. If you do not wish
*   to do so, delete this exception statement from your version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Copyright (c) 2000-2002 RTJ Computing Pty. Ltd. All rights reserved.
**************************************************************************************/
#include "javavm.h"

extern vm_config_t vmconfig;

/****************************************************************************
* Verifies if the requested meory area is valid.
*
* Java prototype:
*       protected native boolean regionOk(int start, int length);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_regionOk(int32 param[], int32 *retval)
{
    *retval = true;
    if ((param[1] >= (int32)vmconfig.heap_start && param[1] < (int32)vmconfig.heap_end) ||
        (param[1]  < (int32)vmconfig.heap_start && (param[1] + param[2]) >= (int32)vmconfig.heap_start))
        *retval = false;
    return NO_EXCEP;
}

/****************************************************************************
* Writes a byte into memory.
*
* Java prototype:
*       protected static native boolean writeByte0(int address, int value);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_writeByte0(int32 param[], int32 *retval)
{
    *retval = true;
    *((uint8 *)param[0]) = (uint8)param[1];
    return NO_EXCEP;
}

/****************************************************************************
* Writes a short into memory.
*
* Java prototype:
*       protected static native boolean writeShort0(int address, int value);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_writeShort0(int32 param[], int32 *retval)
{
    *retval = true;
    *((uint16 *)param[0]) = (uint16)param[1];
    return NO_EXCEP;
}

/****************************************************************************
* Writes an int  into memory.
*
* Java prototype:
*       protected static native boolean writeInt0(int address, int value);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_writeInt0(int32 param[], int32 *retval)
{
    *retval = true;
    *((int32 *)param[0]) = param[1];
    return NO_EXCEP;
}

/****************************************************************************
* Writes a byte array into memory.
*
* Java prototype:
*       protected static native boolean writeBytes0(int address, byte[] src, int start, int length);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_writeBytes0(int32 param[], int32 *retval)
{
    uint8 *arr;
    int16 i;

    *retval = true;
    arr = (uint8 *)vmconfig.vmGetArray(param[1]);
    if (arr != NULL)
    {
        for (i=0; i < param[3]; i++)
            *((uint8 *)(param[0] + i)) = arr[(int16)param[2] + i];
    }
    return NO_EXCEP;
}

/****************************************************************************
* Reads a byte from memory.
*
* Java prototype:
*       protected static native int readByte0(int address);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_readByte0(int32 param[], int32 *retval)
{
    *retval = *((int8 *)param[0]);
    return NO_EXCEP;
}

/****************************************************************************
* Reads a short from memory.
*
* Java prototype:
*       protected static native int readShort0(int address);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_readShort0(int32 param[], int32 *retval)
{
    *retval = *((int16 *)param[0]);
    return NO_EXCEP;
}

/****************************************************************************
* Reads an int from memory.
*
* Java prototype:
*       protected static native int readInt0(int address);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_readInt0(int32 param[], int32 *retval)
{
    *retval = *((int32 *)param[0]);
    return NO_EXCEP;
}

/****************************************************************************
* Reads a byte array from memory.
*
* Java prototype:
*       protected static native void readBytes0(int address, byte[] dest, int start, int length);
*
****************************************************************************/
int16 javax_memory_MemoryRegion_readBytes0(int32 param[], int32 *retval)
{
    uint8 *arr;
    int16 i;

    arr = (uint8 *)vmconfig.vmGetArray(param[1]);
    if (arr != NULL)
    {
        for (i=0; i < param[3]; i++)
            arr[(int16)param[2] + i] = *((int8 *)(param[0] + i));
    }
    return NO_EXCEP;
}

